package com.book.dao;

import com.book.entity.Book;
import com.book.entity.Borrow;
import com.book.entity.Student;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface BookMapper {

    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "bid",property = "book_id"),
            @Result(column = "title",property = "book_name"),
            @Result(column = "time",property = "time"),
            @Result(column = "name",property = "student_name"),
            @Result(column = "sid",property = "student_id")
    })
    @Select("select * from book,borrow,student where borrow.sid=student.sid and borrow.bid=book.bid")
    List<Borrow> getBorrowList();

    @Select("select * from book")
    List<Book> getBookList();

    @Delete("delete from borrow where id=#{id}")
    int deleteBorrowById(@Param("id")int id);

    @Insert("insert into borrow(sid,bid,time) values(#{sid},#{bid},now())")
    int addBorrow(@Param("sid")int sid,@Param("bid")int bid);

    @Delete("delete from book where bid=#{bid}")
    int deleteBookByBid(int bid);

    @Insert("insert into book(title,`desc`,price) values(#{title},#{desc},#{price})")
    int addBook(@Param("title")String title,@Param("desc")String desc,@Param("price")double price);

    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "bid",property = "book_id"),
            @Result(column = "title",property = "book_name"),
            @Result(column = "time",property = "time"),
            @Result(column = "name",property = "student_name"),
            @Result(column = "sid",property = "student_id")
    })
    @Select("select * from book,borrow,student where borrow.sid=student.sid and borrow.bid=book.bid and student.name like concat('%',#{msg},'%')")
    List<Borrow> getBorrowListLike(String msg);

    @Select("select * from book where title like concat('%',#{msg},'%')")
    List<Book> getBookListLike(String msg);

    @Update("update book set title=#{title},`desc`=#{desc},price=#{price} where bid=#{bid}")
    int updateStudent(Book book);
}
